package cn.cs.controller;

import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.pojo.CapitalPoolDetails;
import cn.cs.service.CapitalPoolDetailsService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping(value = "/capitalPoolDetails")
public class CapitalPoolDetailsController {

    @Resource
    private CapitalPoolDetailsService capitalPoolDetailsService;

    /**
     * 分页查询 资金池事件明细
     *
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getCapitalPoolDetailsByPage")
    public Dto getCapitalPoolDetailsByPage(Integer pageNumber, Integer pageSize) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        return DtoUtil.returnSuccess("获取成功", capitalPoolDetailsService.queryCapitalPoolDetailsPageByMap(param, pageNumber, pageSize));
    }


    /**
     * 新增/编辑 资金池事件
     * @param capitalPoolDetails
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateCapitalPoolDetails")
    public Dto updateCapitalPoolDetails(@RequestBody CapitalPoolDetails capitalPoolDetails) {
        try {
            String msg = "新增成功";
            if (EmptyUtils.isEmpty(capitalPoolDetails.getId())) {
                capitalPoolDetails.setMoney(0-capitalPoolDetails.getMoney());
                capitalPoolDetails.setType("0");
                capitalPoolDetailsService.addCapitalPoolDetails(capitalPoolDetails);
            } else {
                msg = "修改成功";
                capitalPoolDetailsService.modifyCapitalPoolDetails(capitalPoolDetails);
            }
            return DtoUtil.returnSuccess(msg);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }

}
